package Object.net.tjdz.view;
import Object.net.tjdz.model.Teacher;
import Object.net.tjdz.service.TeacherService;
import java.util.List;
import java.util.Scanner;

public class TeacherUI {
    private final TeacherService service;
    private final Scanner scanner;

    public TeacherUI(TeacherService service) {
        this.service = service;
        this.scanner = new Scanner(System.in);
    }

    public void showMenu() {
        while (true) {
            System.out.println("\n--- 教师信息管理 ---");
            System.out.println("1. 添加教师");
            System.out.println("2. 删除教师");
            System.out.println("3. 修改教师");
            System.out.println("4. 查询所有教师");
            System.out.println("5. 按工号查询");
            System.out.println("0. 返回主菜单");
            System.out.print("请选择：");

            int choice = getIntInput();

            switch (choice) {
                case 1 -> addTeacher();
                case 2 -> deleteTeacher();
                case 3 -> updateTeacher();
                case 4 -> showAllTeachers();
                case 5 -> findTeacherById();
                case 0 -> { return; }
                default -> System.out.println("无效输入，请重新选择！");
            }
        }
    }

    private void addTeacher() {
        System.out.println("\n--- 添加教师 ---");
        System.out.print("工号：");
        String id = scanner.nextLine();

        if (service.getById(id) != null) {
            System.out.println("该工号已存在！");
            return;
        }

        System.out.print("姓名：");
        String name = scanner.nextLine();

        System.out.print("年龄：");
        int age = getIntInput();
        scanner.nextLine();

        System.out.print("职称：");
        String title = scanner.nextLine();

        Teacher teacher = new Teacher(id, name, age, title);
        if (service.add(teacher)) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private void deleteTeacher() {
        System.out.println("\n--- 删除教师 ---");
        System.out.print("输入要删除的工号：");
        String id = scanner.nextLine();

        if (service.delete(id)) {
            System.out.println("删除成功！");
        } else {
            System.out.println("工号不存在！");
        }
    }

    private void updateTeacher() {
        System.out.println("\n--- 修改教师 ---");
        System.out.print("输入要修改的工号：");
        String id = scanner.nextLine();

        Teacher teacher = service.getById(id);
        if (teacher == null) {
            System.out.println("工号不存在！");
            return;
        }

        System.out.println("当前信息：" + teacher);
        System.out.print("新姓名（留空不变）：");
        String name = scanner.nextLine();
        if (!name.isEmpty()) teacher.setName(name);

        System.out.print("新年龄（留空不变）：");
        String ageInput = scanner.nextLine();
        if (!ageInput.isEmpty()) teacher.setAge(Integer.parseInt(ageInput));

        System.out.print("新职称（留空不变）：");
        String title = scanner.nextLine();

        if (service.update(id, teacher)) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }

    private void showAllTeachers() {
        System.out.println("\n--- 所有教师列表 ---");
        List<Teacher> teachers = service.getAll();

        if (teachers.isEmpty()) {
            System.out.println("没有教师记录");
            return;
        }

        System.out.println("工号\t姓名\t年龄\t职称");
        teachers.forEach(t -> System.out.println(
                t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t" ));
    }

    private void findTeacherById() {
        System.out.println("\n--- 按工号查询 ---");
        System.out.print("输入工号：");
        String id = scanner.nextLine();

        Teacher teacher = service.getById(id);
        if (teacher != null) {
            System.out.println("查询结果：");
            System.out.println(teacher);
        } else {
            System.out.println("工号不存在！");
        }
    }

    private int getIntInput() {
        while (!scanner.hasNextInt()) {
            System.out.print("请输入数字：");
            scanner.next();
        }
        int input = scanner.nextInt();
        scanner.nextLine();
        return input;
    }
}